package com.jt.www.remote;

import com.jt.www.domain.dto.ExcelFileOutputDto;
import com.jt.www.domain.enums.common.parser.ReturnType;
import com.jt.www.domain.enums.common.parser.SaveType;
import com.jt.www.model.reps.GenericDataResponse;
import com.jt.www.model.reps.GenericListResponse;
import com.jt.www.model.vo.ParserResultPolicyAgentVo;
import com.jt.www.model.vo.ParserResultPostRecordedVo;
import com.jt.www.model.vo.endorse.ExcelFileDetailVo;
import com.jt.www.model.vo.endorse.ParserResultVo;
import config.FeignConfiguration;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@FeignClient(value = "annex-parser", url = "${remote.annex_parser_url}",
        configuration = FeignConfiguration.class,
        fallbackFactory = ParserClient.FeignClientFallbackFactory.class)
public interface ParserClient {

    @RequestMapping(method = RequestMethod.POST,
            value = "/analysisExcelByFile",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    ResponseEntity<GenericDataResponse<ParserResultVo>> analysisExcelByFile(
            @PathVariable("excelFile") MultipartFile excelFile,
            @RequestParam("modelCode") String modelCode,
            @RequestParam("returnType") ReturnType returnType,
            @RequestParam("saveType") SaveType saveType,
            @RequestParam(value = "channelCode", defaultValue = "edu") String channelCode) throws Exception;

    @RequestMapping(method = RequestMethod.POST,
            value = "/analysisExcelByFile",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    ResponseEntity<GenericDataResponse<ParserResultPolicyAgentVo>> policyAgentAnalysisExcelByFile(
            @PathVariable("excelFile") MultipartFile excelFile,
            @RequestParam("modelCode") String modelCode,
            @RequestParam("returnType") ReturnType returnType,
            @RequestParam("saveType") SaveType saveType,
            @RequestParam(value = "channelCode", defaultValue = "edu") String channelCode) throws Exception;


    @RequestMapping(method = RequestMethod.POST,
            value = "/analysisExcelByFile",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    ResponseEntity<GenericDataResponse<ParserResultPostRecordedVo>> postRecordedAnalysisExcelByFile(
            @PathVariable("excelFile") MultipartFile excelFile,
            @RequestParam("modelCode") String modelCode,
            @RequestParam("returnType") ReturnType returnType,
            @RequestParam("saveType") SaveType saveType,
            @RequestParam(value = "channelCode", defaultValue = "edu") String channelCode) throws Exception;

    @GetMapping(value = "/excelDetailList",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseEntity<GenericListResponse<ExcelFileDetailVo>> excelDetailList(
            @RequestParam(value = "fileId") String fileId,
            @RequestParam(value = "field3", required = false) String field3,
            @RequestParam(value = "pn", defaultValue = "1") int pn,
            @RequestParam(value = "ps", defaultValue = "10") int ps
    ) throws Exception;

    @GetMapping(value = "/excelFileDtoList",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseEntity<GenericDataResponse<ExcelFileOutputDto>> excelFileDtoList(
            @RequestParam(value = "excelFileId") String excelFileId
    ) throws Exception;


    @Component
    class FeignClientFallbackFactory implements FallbackFactory<ParserClient> {

        public static final Logger log = LoggerFactory.getLogger(FeignClientFallbackFactory.class);

        @Override
        public ParserClient create(Throwable cause) {
            return new ParserClient() {
                @Override
                public ResponseEntity<GenericDataResponse<ParserResultVo>> analysisExcelByFile(MultipartFile excelFile, String modelCode, ReturnType returnType, SaveType saveType, String channelCode) {
                    log.error("上传文件到清单服务中心失败接口analysisExcelByFile,fallback reason:", cause);
                    return null;
                }

                @Override
                public ResponseEntity<GenericDataResponse<ParserResultPolicyAgentVo>> policyAgentAnalysisExcelByFile(MultipartFile excelFile, String modelCode, ReturnType returnType, SaveType saveType, String channelCode) throws Exception {
                    log.error("上传文件到清单服务中心失败接口policyAgentAnalysisExcelByFile,fallback reason:", cause);
                    return null;
                }

                @Override
                public ResponseEntity<GenericDataResponse<ParserResultPostRecordedVo>> postRecordedAnalysisExcelByFile(MultipartFile excelFile, String modelCode, ReturnType returnType, SaveType saveType, String channelCode) {
                    log.error("上传文件到清单服务中心失败接口postRecordedAnalysisExcelByFile,fallback reason:", cause);
                    return null;
                }

                @Override
                public ResponseEntity<GenericListResponse<ExcelFileDetailVo>> excelDetailList(String fileId, String field3, int pn, int ps) throws Exception {
                    log.error("从清单服务中心读取数据失败接口excelDetailList,fallback reason:", cause);
                    return null;
                }

                @Override
                public ResponseEntity<GenericDataResponse<ExcelFileOutputDto>> excelFileDtoList(String excelFileId) throws Exception {
                    log.error("从清单服务中心读取数据失败接口excelFileDtoList,fallback reason:", cause);
                    return null;
                }
            };
        }
    }
}
